package com.xkstream.client.presenter.kt

import com.xkstream.client.api.kt.MyZoneService
import com.xkstream.client.base.BaseData
import com.xkstream.client.base.BasePresenterImpl
import com.xkstream.client.entity.kt.CommentListData
import com.xkstream.client.rx.RxHelper
import com.xkstream.client.rx.RxObserver
import com.xkstream.client.utils.RetrofitUtil

/**
 * Created by xiaoyi on 2017-11-4.
 */
class MyZoneDetailPresenter() : MyZoneDetailContract.Presenter, BasePresenterImpl() {



    var view: MyZoneDetailContract.View? = null
    var myzoneService: MyZoneService? = null

    constructor(view: MyZoneDetailContract.View) : this() {
        this.view = view
        this.myzoneService = RetrofitUtil.getRetrofit().create(MyZoneService::class.java)
    }

    override fun getCommentList(params: Map<String, String>) {
        view?.showLoading()
        myzoneService?.getCommentList(params)
                ?.compose(RxHelper.toUI())
                ?.subscribe(object : RxObserver<BaseData<CommentListData?>>(this) {
                    override fun onFail(message: String) {
                        view?.hideLoading()
                        view?.showError(message)
                    }

                    override fun onSuccess(t: BaseData<CommentListData?>) {
                        view?.hideLoading()
                        view?.commentList(t)
                    }
                })
    }

    override fun publishComment(params: Map<String, String>) {
        view?.showLoading()
        myzoneService?.publishComment(params)
                ?.compose(RxHelper.toUI())
                ?.subscribe(object : RxObserver<BaseData<Any?>>(this) {
                    override fun onFail(message: String) {
                        view?.hideLoading()
                        view?.showError(message)
                    }

                    override fun onSuccess(t: BaseData<Any?>) {
                        view?.hideLoading()
                        view?.showCommentResult(t)
                    }
                })
    }

    override fun blogUpVote(params: Map<String, String>) {
        view?.showLoading()
        myzoneService?.blogUpVote(params)
                ?.compose(RxHelper.toUI())
                ?.subscribe(object : RxObserver<BaseData<Any?>>(this) {
                    override fun onFail(message: String) {
                        view?.hideLoading()
                        view?.showError(message)
                    }

                    override fun onSuccess(t: BaseData<Any?>) {
                        view?.hideLoading()
                        view?.showUpVoteResult(t)
                    }
                })
    }

    override fun deleteBlog(params: Map<String, String>) {
        view?.showLoading()
        myzoneService?.deleteBlog(params)
                ?.compose(RxHelper.toUI())
                ?.subscribe(object :RxObserver<BaseData<Any?>>(this){
                    override fun onSuccess(t: BaseData<Any?>) {
                        view?.hideLoading()
                        view?.showDeleteBlogResult(t)
                    }

                    override fun onFail(message: String?) {
                        view?.hideLoading()
                    }
                })
    }

}